package com.oneplatform.user.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.mybatis.plugin.pagination.PageExecutor;
import com.jeesuite.mybatis.plugin.pagination.PageExecutor.PageDataLoader;
import com.jeesuite.mybatis.plugin.pagination.PageParams;
import com.oneplatform.base.exception.AssertUtil;
import com.oneplatform.base.model.PageResult;
import com.oneplatform.user.dao.entity.UserInfoEntity;
import com.oneplatform.user.dao.mapper.UserInfoEntityMapper;

/**
 * generated by www.jeesuite.com
 */
@Service
public class UsersService {

	@Autowired
	private UserInfoEntityMapper usersMapper;

	public void addUsers(UserInfoEntity entity) {
		usersMapper.insertSelective(entity);
	}

	public void updateUsers(UserInfoEntity entity) {
		UserInfoEntity originEntity = usersMapper.selectByPrimaryKey(entity.getId());
		AssertUtil.notNull(originEntity);
		BeanUtils.copy(entity, originEntity);
		usersMapper.updateByPrimaryKey(originEntity);
	}
	
	public void deleteUsers(Integer id) {
		usersMapper.deleteByPrimaryKey(id);
	}
	
	public UserInfoEntity findUsersById(Integer id){
		UserInfoEntity entity = usersMapper.selectByPrimaryKey(id);
		AssertUtil.notNull(entity);
		return entity;
	}
	
    public PageResult<UserInfoEntity> pageQuery(PageParams pageParam,Map<String, Object> conditions){
		Page<UserInfoEntity> page = PageExecutor.pagination(pageParam, new PageDataLoader<UserInfoEntity>() {
			@Override
			public List<UserInfoEntity> load() {
				return usersMapper.findByParam(conditions);
			}
		});
		return new PageResult<UserInfoEntity>(page.getPageNo(), page.getPageSize(), page.getTotal(), page.getData());
	}

}
